<template>
    <div class="login">
        <div class="box">
             <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pwd">
                    <el-input v-model="ruleForm.pwd"></el-input>
                </el-form-item>
                 <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
             </el-form>   
        </div>
    </div>
</template>
<script>
 var checkPwd = (rule, value, callback) => {
    // value就是你校验的内容
    // callback()  校验通过
    // callback(new Error());校验不通过
        if (!value) {
          return callback(new Error('密码不能为空'));
        }else{
            let reg = /^[a-zA-Z0-9]{6}$/
            if(!reg.test(value)){
                console.log('不通过');
               return callback(new Error('密码必须是6位'));
            }else{
                console.log('通过');
                callback()
            }
        }
 }
export default {
    data() {
        return {
            ruleForm:{},
            rules:{
                 username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },

                ],
                pwd:[
                    // { required: true, message: '请输入用户名', trigger: 'blur' },
                     {required: true, validator: checkPwd, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
              this.$router.push('/')
              localStorage.setItem('token',new Date().getTime())
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
}
</script>
<style lang="scss" scoped>
.login{
    display: flex;
    align-items: center;
    justify-content:center;
    height: 100vh;
    width:100%;
    background: seagreen;
   
    .box{
        width:500px;
        height: 300px;
        background: #fff;
         padding: 30px;
    }
}
</style>
